package com.diqizu.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.diqizu.entity.ChanPininof;
import com.diqizu.entity.DisPlay;
import com.diqizu.entity.ProType;
import com.diqizu.utli.DBManager;
import com.diqizu.utli.QueryDataResult;
import com.mysql.jdbc.StringUtils;

/**
 * 浜у搧灞曠ず椤甸潰 zmx
 * 
 * @author DELL
 * @param <QueryDataResult>
 *
 */

public class DisPlayDaoImpl {
	// 获取全部详情,头部方法
	public ArrayList<DisPlay> DisPlay() {
		String sql = "select * from product";

		System.out.println("执行的语句是" + sql);
		QueryDataResult execQuery = DBManager.execQuery(sql);
		ArrayList<DisPlay> list = new ArrayList<DisPlay>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					DisPlay e1 = new DisPlay(rs.getInt("proid"), rs.getString("name"), rs.getString("style"),
							rs.getString("color"), rs.getString("Brand"), rs.getString("Price"),
							rs.getString("dingwei"), rs.getString("meterial"), rs.getString("diZhi"),
							rs.getInt("productTypeId"), rs.getString("proUrl"), rs.getString("isShowHome"),
							rs.getString("zuzhuang"), rs.getString("sPrice"));

					list.add(e1);
				}
			} catch (SQLException e1) {
				e1.printStackTrace();

			} finally {

				execQuery.close();
			}
		}

		return list;
	}

	// 跳转单个物品详情方法
	public ArrayList<DisPlay> displayb(int id) {
		String sql = "select * from product where proid=" + id;
		System.out.println("执行的语句是：" + sql);
		QueryDataResult execQuery = DBManager.execQuery(sql);
		ArrayList<DisPlay> list = new ArrayList<DisPlay>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					DisPlay e1 = new DisPlay(rs.getInt("proid"), rs.getString("name"), rs.getString("style"),
							rs.getString("color"), rs.getString("Brand"), rs.getString("Price"),
							rs.getString("dingwei"), rs.getString("meterial"), rs.getString("diZhi"),
							rs.getInt("productTypeId"), rs.getString("proUrl"), rs.getString("isShowHome"),
							rs.getString("zuzhuang"), rs.getString("sPrice"));

					list.add(e1);
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		return list;
	}

	// 获取单个展示
	public ArrayList<DisPlay> displayc(String productTypeId) {
		String sql = "select * from product";
		if (productTypeId != null && !productTypeId.equals("")) {
			sql = sql + " where productTypeId='" + productTypeId + "' ";
		}
		
		System.out.println("执行的语句是" + sql);
		QueryDataResult execQuery = DBManager.execQuery(sql);
		ArrayList<DisPlay> list = new ArrayList<DisPlay>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					DisPlay e1 = new DisPlay(rs.getInt("proid"), rs.getString("name"), rs.getString("style"),
							rs.getString("color"), rs.getString("Brand"), rs.getString("Price"),
							rs.getString("dingwei"), rs.getString("meterial"), rs.getString("diZhi"),
							rs.getInt("productTypeId"), rs.getString("proUrl"), rs.getString("isShowHome"),
							rs.getString("zuzhuang"), rs.getString("sPrice"));
					list.add(e1);
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		return list;
	}

	public ArrayList<DisPlay> showSearch(String productTypeId, String searchName) {
		// sql语句
		String sql = "select * from product where 1=1";
		if (productTypeId != null && !productTypeId.equals("")) {
			sql = sql + " and productTypeId=" + productTypeId;
		}
		if (searchName != null && !searchName.equals("")) {
			sql = sql + " and name like'%" + searchName + "%'";
		}
		System.out.println("执行的sql语句：" + sql);
		// 使用封装的方法
		QueryDataResult execQuery = DBManager.execQuery(sql);
		ArrayList<DisPlay> list = new ArrayList<DisPlay>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					DisPlay e1 = new DisPlay(rs.getInt("proid"), rs.getString("name"), rs.getString("style"),
							rs.getString("color"), rs.getString("Brand"), rs.getString("Price"),
							rs.getString("dingwei"), rs.getString("meterial"), rs.getString("diZhi"),
							rs.getInt("productTypeId"), rs.getString("proUrl"), rs.getString("isShowHome"),
							rs.getString("zuzhuang"), rs.getString("sPrice"));

					list.add(e1);
				}
			} catch (SQLException typeImgs) {
				typeImgs.printStackTrace();
			}
			execQuery.close();
		}
		return list;

	}

	// 插入的方法
	public boolean insertproduct(DisPlay DisPlay) {

		String sql = "insert into product (name,Price,Brand,proUrl,dingwei,meterial,diZhi,productTypeId,style,isShowHome,sPrice)"
				+ " values('" + DisPlay.getName() + "','" + DisPlay.getPrice() + "','" + DisPlay.getBrand() + "','"
				+ DisPlay.getProUrl() + "','" + DisPlay.getDingwei() + "','" + DisPlay.getMeterial() + "','"
				+ DisPlay.getDiZhi() + "','" + DisPlay.getProductTypeId() + "','" + DisPlay.getStlye() + "','"
				+ DisPlay.getIsShowHome() + "','" + DisPlay.getsPrice() + "')";

		System.out.println("执行的语句是" + sql);
		return DBManager.execUpdate(sql);

	}

	// 修改方法
	public boolean updateproduct(DisPlay DisPlay) {
		String sql = "update product set name='" + DisPlay.getName() + "',Price='" + DisPlay.getPrice() + "',Brand='"
				+ DisPlay.getBrand() + "',proUrl='" + DisPlay.getProUrl() + "',dingwei='" + DisPlay.getDingwei()
				+ "',meterial='" + DisPlay.getMeterial() + "',diZhi='" + DisPlay.getDiZhi() + "',productTypeId='"
				+ DisPlay.getProductTypeId() + "'" + ",style='" + DisPlay.getStlye() + "',isShowHome='"
				+ DisPlay.getIsShowHome() + "',sPrice='" + DisPlay.getsPrice() + "' where proid='" + DisPlay.getProid()
				+ "'";
		System.out.println("执行的语句是：" + sql);
		return DBManager.execUpdate(sql);
	}

	// 删除
	public boolean deleteproduct(int proid) {
		String sql = "delete from product where proid =" + proid;
		System.out.println("执行的语句是：" + sql);
		return DBManager.execUpdate(sql);

	}

	public ArrayList<DisPlay> chaproduct(String name, String productTypeId, String minprice, String maxprice,
			String page, String limit) {
		String sql = "select product.* ,producttype.typeName as typeName from product left join producttype on product.productTypeId =producttype.typeId where 1=1";
		// 动态拼接sql语句
		if (name != null && !name.equals("")) {
			sql += " and product.name like '%" + name + "%' ";
		}
		if (!StringUtils.isNullOrEmpty(productTypeId)) {
			sql += " and productTypeId = '" + productTypeId + "' ";
		}
		if (!StringUtils.isNullOrEmpty(minprice)) {
			sql += " and Price >=  " + minprice;
		}
		if (!StringUtils.isNullOrEmpty(maxprice)) {
			sql += " and Price <=  " + maxprice;
		}
		if (!StringUtils.isNullOrEmpty(page) && !StringUtils.isNullOrEmpty(page)) {
			int p = Integer.valueOf(page);
			int c = Integer.valueOf(limit);
			int start = (p - 1) * c;
			sql += " limit  " + start + ", " + c;
		}
		System.out.println("执行的sql语句是:" + sql);
		QueryDataResult execQuery = DBManager.execQuery(sql);
		ArrayList<DisPlay> list = new ArrayList<DisPlay>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					DisPlay e1 = new DisPlay(rs.getInt("proid"), rs.getString("name"), rs.getString("style"),
							rs.getString("color"), rs.getString("Brand"), rs.getString("Price"),
							rs.getString("dingwei"), rs.getString("meterial"), rs.getString("diZhi"),
							rs.getInt("productTypeId"), rs.getString("proUrl"), rs.getString("isShowHome"),
							rs.getString("zuzhuang"), rs.getString("sPrice"));

					list.add(e1);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println("查到的数据是：" + list);
		return list;
	}
	// 下拉系列

	public ArrayList<ProType> gettype() {
		String sql = "select * from producttype";

		System.out.println("ִ执行的语句是" + sql);
		QueryDataResult execQuery = DBManager.execQuery(sql);
		ArrayList<ProType> list = new ArrayList<ProType>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					ProType e1 = new ProType(rs.getInt("typeId"), rs.getString("typeName"),
							rs.getString("productTypeId"));

					list.add(e1);
				}
			} catch (SQLException e1) {
				e1.printStackTrace();

			} finally {

				execQuery.close();
			}
		}
		System.out.println(list);
		return list;
	}

}